package com.dongdong.aop;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component     //放到容器里
@Aspect       // Aop的五大通知
@Slf4j         //需要导入依赖 lombok小辣椒
public class AopLog {

    //就是代替将来需要打印日志的所有方法
    @Pointcut("execution(public * com.dongdong.service.*.*(..)) || @annotation(com.dongdong.aop.LogAn)")

    public void log(){}

    //前置通知
    @Before("log()")public void beforeLog(JoinPoint point){
        //获取方法名
        Signature signature = point.getSignature();
        //获取参数
        Object[] args = point.getArgs();
        log.info("正在执行的方法{},参数为{}",signature,args);
    }

    //返回后通知
    @AfterReturning(value = "log()",returning = "result")
    public void afterReturningLog(Object result){
        log.info("方法的返回值为{}", JSONObject.toJSONString(result));
    }







}
